<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Global fixture</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Boost.Test">
<link rel="up" href="../fixtures.html" title="Fixtures">
<link rel="prev" href="per_test_suite_fixture.html" title="Test suite entry/exit fixture">
<link rel="next" href="../tests_dependencies.html" title="Managing test dependencies">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="per_test_suite_fixture.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fixtures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../tests_dependencies.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_test.tests_organization.fixtures.global"></a><a class="link" href="global.html" title="Global fixture">Global
        fixture</a>
</h4></div></div></div>
<p>
          Any global initialization that needs to be performed before any test begins,
          or a cleanup that is to be performed after all tests are finished is called
          a <span class="emphasis"><em>global fixture</em></span>. A global fixture is equivalent to
          a <a class="link" href="per_test_suite_fixture.html" title="Test suite entry/exit fixture">test-suite
          entry/exit</a> fixture (executed once), where in this case the test-suite
          is the <a class="link" href="../test_tree/master_test_suite.html" title="Master test suite">master
          test suite</a>.
        </p>
<p>
          The <span class="emphasis"><em>Unit Test Framework</em></span> global fixture design is based
          on the <a class="link" href="models.html" title="Fixture models">generic
          test class fixture model</a>. The global fixture design allows any number
          of global fixtures to be defined in any test file that constitutes a test
          module. Though some initialization can be implemented in the <a class="link" href="../../adv_scenarios/test_module_init_overview.html" title="Test module's initialization">test
          module initialization function</a>, there are several reasons to prefer
          the global fixture approach:
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              There is no place for <code class="computeroutput"><span class="identifier">cleanup</span></code>/<code class="computeroutput"><span class="identifier">teardown</span></code> operations in the initialization
              function.
            </li>
<li class="listitem">
              Unlike the initialization function, the global fixture construction,
              <code class="computeroutput"><span class="identifier">setup</span></code> and <code class="computeroutput"><span class="identifier">teardown</span></code> methods invocation are guarded
              by the execution monitor. That means that all uncaught errors that
              occur during initialization are properly reported.
            </li>
<li class="listitem">
              Any number of different global fixtures can be defined, which allows
              you to split initialization code by category.
            </li>
<li class="listitem">
              The fixture allows you to place matching <code class="computeroutput"><span class="identifier">setup</span></code>/<code class="computeroutput"><span class="identifier">teardown</span></code> code in close vicinity in
              your test module code.
            </li>
<li class="listitem">
              If the whole test tree is constructed automatically, the initialization
              function is empty and auto-generated by the <span class="emphasis"><em>Unit Test Framework</em></span>.
              Introducing the initialization function can be more work than using
              the global fixture facility, while global fixture is more to the point.
            </li>
<li class="listitem">
              Since all fixtures follow the same generic model you can easily switch
              from local per test case fixtures to the global one.
            </li>
</ul></div>
<p>
          To define a global test module fixture you need:
        </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
              to implement a class that matches the <a class="link" href="models.html" title="Fixture models">fixture
              model</a>
            </li>
<li class="listitem">
              and to pass the class as an argument to the macro <a class="link" href="../../utf_reference/test_org_reference/test_org_boost_test_global_fixture.html" title="BOOST_TEST_GLOBAL_FIXTURE"><code class="computeroutput"><span class="identifier">BOOST_TEST_GLOBAL_FIXTURE</span></code></a>.
            </li>
</ol></div>
<pre class="programlisting"><span class="identifier">BOOST_TEST_GLOBAL_FIXTURE</span><span class="special">(</span> <span class="identifier">fixture_name</span> <span class="special">);</span>

<span class="comment">// if your fixture is within a namespace</span>
<span class="keyword">namespace</span> <span class="identifier">enclosing_namespace</span> <span class="special">{</span>
  <span class="keyword">struct</span> <span class="identifier">fixture_in_namespace</span> <span class="special">{</span> <span class="comment">/* ... */</span> <span class="special">};</span>
<span class="special">}</span>

<span class="keyword">namespace</span> <span class="identifier">enclosing_namespace</span> <span class="special">{</span>
  <span class="identifier">BOOST_TEST_GLOBAL_FIXTURE</span><span class="special">(</span> <span class="identifier">fixture_in_namespace</span> <span class="special">);</span>
<span class="special">}</span>
</pre>
<div class="caution"><table border="0" summary="Caution">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../../doc/src/images/caution.png"></td>
<th align="left">Caution</th>
</tr>
<tr><td align="left" valign="top"><p>
            The statement defining the global fixture has to reside in a single compilation
            unit (no header).
          </p></td></tr>
</table></div>
<div class="caution"><table border="0" summary="Caution">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../../doc/src/images/caution.png"></td>
<th align="left">Caution</th>
</tr>
<tr><td align="left" valign="top"><p>
            Beware of other fixtures in other compilation units having the same name:
            if the fixtures have different definition, this leads to an ODR violation.
          </p></td></tr>
</table></div>
<h6>
<a name="boost_test.tests_organization.fixtures.global.h0"></a>
          <span class="phrase"><a name="boost_test.tests_organization.fixtures.global.example_descr"></a></span><a class="link" href="global.html#boost_test.tests_organization.fixtures.global.example_descr">Example:
          Global fixture</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">fixture_04</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">struct</span> <span class="identifier">MyGlobalFixture</span> <span class="special">{</span>
  <span class="identifier">MyGlobalFixture</span><span class="special">()</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"ctor fixture i="</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">);</span>
  <span class="special">}</span>
  <span class="keyword">void</span> <span class="identifier">setup</span><span class="special">()</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"setup fixture i="</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">);</span>
      <span class="identifier">i</span><span class="special">++;</span>
  <span class="special">}</span>
  <span class="keyword">void</span> <span class="identifier">teardown</span><span class="special">()</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"teardown fixture i="</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">);</span>
      <span class="identifier">i</span> <span class="special">+=</span> <span class="number">2</span><span class="special">;</span>
  <span class="special">}</span>
  <span class="special">~</span><span class="identifier">MyGlobalFixture</span><span class="special">()</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span> <span class="string">"dtor fixture i="</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">);</span>
  <span class="special">}</span>
  <span class="keyword">static</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">;</span>
<span class="special">};</span>
<span class="keyword">int</span> <span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

<span class="identifier">BOOST_TEST_GLOBAL_FIXTURE</span><span class="special">(</span> <span class="identifier">MyGlobalFixture</span> <span class="special">);</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case1</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span><span class="string">"running test_case1"</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case2</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span><span class="string">"running test_case2"</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">fixture_04</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">message</span>
<span class="identifier">Running</span> <span class="number">2</span> <span class="identifier">test</span> <span class="identifier">cases</span><span class="special">...</span>
<span class="identifier">ctor</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span>
<span class="identifier">setup</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span>
<span class="identifier">running</span> <span class="identifier">test_case1</span>
<span class="identifier">running</span> <span class="identifier">test_case2</span>
<span class="special">./</span><span class="identifier">fixture_04</span><span class="special">.</span><span class="identifier">run</span><span class="special">-</span><span class="identifier">fail</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">42</span><span class="special">:</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case2"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">MyGlobalFixture</span><span class="special">::</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">3</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">1</span> <span class="special">!=</span> <span class="number">3</span><span class="special">]</span>
<span class="identifier">teardown</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">1</span>
<span class="identifier">dtor</span> <span class="identifier">fixture</span> <span class="identifier">i</span><span class="special">=</span><span class="number">3</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failure</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"fixture_04"</span>
</pre>
                </td></tr></tbody>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2022 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="per_test_suite_fixture.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fixtures.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../tests_dependencies.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
